<?php

echo "Starting multivalue field: Största exportmarknader";

include_once('config.inc.php');

$link=mysql_connect($hostname,$username,$password); 

$status=mysql_select_db("vtigercrm520");

$query="SELECT accountid,DATA,Key1 FROM KONTAKTDATA WHERE Field = 45";

$mvaluesquery=mysql_query($query, $link);

$aidvalues=array();
$countries=array();

if(!$mvaluesquery) {
		print("Query Error: ".mysql_error()."<br/>");
	}

while($data=mysql_fetch_row($mvaluesquery)) {
	$aid=$data[0];
	$value=$data[1];
	$countries[$value]=0;
	print("fetching aid $aid value $value and Key1 = $data[2]  <br/>");
    if(empty($aidvalues[$aid])){
		$aidvalues[$aid]=$value;
	} else {
		$aidvalues[$aid]=$aidvalues[$aid]." |##| ".$value;
		}
}          

unset($aidvalues['0']);

foreach ($aidvalues as $aid => $value) {
	print("Adding to $aid the following multivalues: $value <br/>");
	$updatequery="UPDATE vtiger_accountscf SET cf_682 = '$value' WHERE accountid = '$aid'";
	$execupdatequery=mysql_query($updatequery, $link);
	if(!$execupdatequery) {
		print("Query Error: ".mysql_error()."<br/>");
	}
	
	
	}

foreach ($countries as $key => $value) {
	print($key."<br/>");
	}
	
mysql_close($link);
?>
